<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace addons\cloud_market\controller\api\developer;

use addons\cloud_market\controller\api\CloudMarketApiController;
use addons\cloud_market\model\CloudMarketAddon;
use addons\cloud_market\model\CloudMarketAddonVersions;
use addons\cloud_market\model\CloudMarketSiteAddon;
 use hiduoke\services\FormBuilder as Form;

class SiteAddon extends CloudMarketApiController
{
    protected $notApiCheck = ['*'];

    public function initialize()
    {
        parent::initialize();
        $this->model = new CloudMarketSiteAddon();
        $this->version_model = new CloudMarketAddonVersions();
        $user = $this->getUser();
        if (!$user['developer']) {
           return $this->error('您没有权限');
        }
        $this->dev_id = $user['developer']['id'];
        $this->user_id = $user['id'];
    }

    /**
     * 【商城】获取列表
     */
    public function list()
    {
        // 获取当前用户有权限的订单
        $aids = (new CloudMarketAddon())->where(['user_id' => $this->user_id, 'dev_id' => $this->dev_id])->column('id');
        $mix['$where'] = [['addon_id', 'in', $aids]];
        $res = $this->model->getPageList($mix);
        $data = $res['data']->append(['addon', 'site', 'used_count', 'servicetime_man']);
        foreach ($data as $key => $value) {
            $data[$key]['servicetime'] = $value->servicetime;
        }
        return $this->success($res['count'], $data);
    }

    public function getForm($id)
    {
        $row = $this->model->findOrEmpty($id);//创建表单
        $action = $this->request->domain() . "/api.developer.site_addon/save?id=" . $id;
        $form = Form::createForm($action)->setMethod("POST");
        //新增表单
        $rule = [
            Form::dkInputNumber('count', '激活次数', $row['count'] ?? 0)->required(),
            Form::datetime('servicetime', '服务日期', $row['servicetime'])->valueFormat('X'),
            Form::radio('status', '状态', $row['status'] ?? '0')
                ->options([['label' => '正常', 'value' => '0'], ['label' => '禁用', 'value' => '-1']]),
        ];
        $title = !$id ? "新增" : "编辑";
        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    public function save($id = 0)
    {
        $row = $this->model->findOrEmpty($id);
        $formData = getFormData();
        $row->save($formData);
       return $this->success("保存成功", $formData);
    }

    public function addServiceTimeForm($id)
    {
        $row = $this->model->findOrEmpty($id);//创建表单
        $action = $this->request->domain() . "/api.developer.site_addon/saveAddServiceTime?id=" . $id;
        $form = Form::createForm($action)->setMethod("POST");
        //新增表单
        $rule = [
            Form::dkInputNumber('month', '增加月份', $row['month'] ?? 12)->required(),
        ];
        $title = !$id ? "新增" : "编辑";
        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    public function saveAddServiceTime($id = 0)
    {
        $row = $this->model->findOrEmpty($id);
        $month = getFormData()['month'];
        $currentServiceTime = time();
        $newServiceTime = strtotime("+$month months", $currentServiceTime);
        $data = [
            'servicetime' => $newServiceTime
        ];
        $row->save($data);
        return $this->success("保存成功");
    }

}
